package com.example.syyao.myapp.Sign;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.example.syyao.myapp.MainActivity;
import com.example.syyao.myapp.R;
import com.example.syyao.myapp.Response.Get_response;
import com.example.syyao.myapp.WebContact.JsonUtil;
import com.example.syyao.myapp.WebContact.WebAccessTools;

public class Sign_in extends AppCompatActivity {

    Get_response sign_in_response;
    TextView sign_up;
    Button sign_in;
    TextView username;
    TextView password;
//    public static Get_response_List UserInformation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign_in);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        sign_up = findViewById(R.id.sign_up_in_sign_in);
        sign_up.setOnClickListener(new sign_up_click());

        sign_in = findViewById(R.id.button_sign_in);
        sign_in.setOnClickListener(new sign_in_click());

        username = findViewById(R.id.sign_in_phone);
        password = findViewById(R.id.sign_in_password);

    }

    private class sign_up_click implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(Sign_in.this, Sign_up.class);
            startActivity(intent);
            finish();
        }
    }

    private class sign_in_click implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if (username.getText().toString().equals("")) {
                Toast.makeText(Sign_in.this, getResources().getString(R.string.username), Toast.LENGTH_SHORT).show();
                return;
            }
            if (password.getText().toString().equals("")) {
                Toast.makeText(Sign_in.this, getResources().getString(R.string.password), Toast.LENGTH_SHORT).show();
                return;
            }
            login();
//              Intent intent = new Intent();
//              intent.setClass(Sign_in.this,MainActivity.class);
//              startActivity(intent);
        }
    }

    private void login() {
        new Thread(new Runnable() {

            @Override
            public void run() {

                String webContent = WebAccessTools.login(username.getText().toString(),password.getText().toString());
                System.out.println(webContent);
                sign_in_response= (Get_response) JsonUtil.jsonToObject(webContent,
                        Get_response.class);
                handler.sendEmptyMessage(0x123);
            }
        }).start();

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (sign_in_response != null && sign_in_response.getMessage().equals("成功")) {
                Toast.makeText(Sign_in.this, "发送成功！",Toast.LENGTH_SHORT).show();

                SharedPreferences sharedPreferences=getSharedPreferences(MainActivity.PREFERENCE_NAME,MainActivity.MODE);
                SharedPreferences.Editor editor=sharedPreferences.edit();
                editor.putBoolean("isLogin",true);
                editor.putString("username",username.getText().toString());
                editor.putString("password",password.getText().toString());
                editor.apply();
                Intent intent = new Intent(Sign_in.this,MainActivity.class);
                startActivity(intent);
            } else {
                Toast.makeText(Sign_in.this, "用户名或者密码有误！",Toast.LENGTH_SHORT).show();

            }
        };
    };
}
